package com.ruoyi.studyroom.bpm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.studyroom.bpm.enums.ProdNoticeConfirmStateTypes;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.studyroom.bpm.domain.bo.BpmProductionNoticeBo;
import com.ruoyi.studyroom.bpm.domain.vo.BpmProductionNoticeVo;
import com.ruoyi.studyroom.bpm.domain.BpmProductionNotice;
import com.ruoyi.studyroom.bpm.mapper.BpmProductionNoticeMapper;
import com.ruoyi.studyroom.bpm.service.IBpmProductionNoticeService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 生产通知单Service业务层处理
 *
 * @author hubo_dxy
 * @date 2025-07-07
 */
@RequiredArgsConstructor
@Service
public class BpmProductionNoticeServiceImpl implements IBpmProductionNoticeService {

    private final BpmProductionNoticeMapper baseMapper;

    /**
     * 查询生产通知单
     *
     * @param id 生产通知单主键
     * @return 生产通知单
     */
    @Override
    public BpmProductionNoticeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询生产通知单列表
     *
     * @param bo 生产通知单
     * @return 生产通知单
     */
    @Override
    public TableDataInfo<BpmProductionNoticeVo> queryPageList(BpmProductionNoticeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BpmProductionNotice> lqw = buildQueryWrapper(bo);
        Page<BpmProductionNoticeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询生产通知单列表
     *
     * @param bo 生产通知单
     * @return 生产通知单
     */
    @Override
    public List<BpmProductionNoticeVo> queryList(BpmProductionNoticeBo bo) {
        LambdaQueryWrapper<BpmProductionNotice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BpmProductionNotice> buildQueryWrapper(BpmProductionNoticeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BpmProductionNotice> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDNo()), BpmProductionNotice::getDNo, bo.getDNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDVer()), BpmProductionNotice::getDVer, bo.getDVer());
        lqw.eq(StringUtils.isNotBlank(bo.getBpmCheckState()), BpmProductionNotice::getBpmCheckState, bo.getBpmCheckState());
        String confirmState = bo.getConfirmState();
        if (StringUtils.isNotBlank(confirmState)) {
            if ("all".equals(confirmState)) {
                lqw.in(BpmProductionNotice::getConfirmState, ProdNoticeConfirmStateTypes.getAllStateCode());

            } else {
                lqw.eq(BpmProductionNotice::getConfirmState, confirmState);
            }
        }
        lqw.eq(StringUtils.isNotBlank(bo.getDSupplier()), BpmProductionNotice::getDSupplier, bo.getDSupplier());
        lqw.eq(bo.getBpmSqdate() != null, BpmProductionNotice::getBpmSqdate, bo.getBpmSqdate());
        lqw.eq(bo.getBpmBusinesskey() != null, BpmProductionNotice::getBpmBusinesskey, bo.getBpmBusinesskey());
        lqw.eq(StringUtils.isNotBlank(bo.getBpmBusinesstype()), BpmProductionNotice::getBpmBusinesstype, bo.getBpmBusinesstype());
        lqw.eq(StringUtils.isNotBlank(bo.getBpmAttachmentUrl()), BpmProductionNotice::getBpmAttachmentUrl, bo.getBpmAttachmentUrl());
        return lqw;
    }

    /**
     * 新增生产通知单
     *
     * @param bo 生产通知单
     * @return 结果
     */
    @Override
    public Boolean insertByBo(BpmProductionNoticeBo bo) {
        BpmProductionNotice add = BeanUtil.toBean(bo, BpmProductionNotice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改生产通知单
     *
     * @param bo 生产通知单
     * @return 结果
     */
    @Override
    public Boolean updateByBo(BpmProductionNoticeBo bo) {
        BpmProductionNotice update = BeanUtil.toBean(bo, BpmProductionNotice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BpmProductionNotice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除生产通知单
     *
     * @param ids 需要删除的生产通知单主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
